// EmployeeTableDlg.cpp : implementation file
//

#include "stdafx.h"
#include "backManage.h"
#include "EmployeeTableDlg.h"
#include "backManageDlg.h"


// CEmployeeTableDlg dialog

IMPLEMENT_DYNAMIC(CEmployeeTableDlg, CMaxDisplayDlg)

CEmployeeTableDlg::CEmployeeTableDlg(CWnd* pParent /*=NULL*/)
	: CMaxDisplayDlg(CEmployeeTableDlg::IDD, pParent)
{

}

CEmployeeTableDlg::~CEmployeeTableDlg()
{
}

void CEmployeeTableDlg::DoDataExchange(CDataExchange* pDX)
{
	CMaxDisplayDlg::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_EPY_INDEX, m_nEmployee.Index);
	DDX_Text(pDX, IDC_EDIT_EPY_NAME, m_nEmployee.Name);
	DDX_Text(pDX, IDC_EDIT_EPY_ATTR, m_nEmployee.Attr);
	DDX_Text(pDX, IDC_EDIT_EPY_PASSWORD, m_nEmployee.Password);
	DDX_Text(pDX, IDC_EDIT_EPY_MASTER, m_nEmployee.Master);
	DDX_Text(pDX, IDC_EDIT_EPY_SCHOOL, m_nEmployee.School);
	DDX_Text(pDX, IDC_EDIT_EPY_CARDNO, m_nEmployee.CardNO);
	DDX_Text(pDX, IDC_EDIT_EPY_PHONE1, m_nEmployee.Phone1);
	DDX_Text(pDX, IDC_EDIT_EPY_PHONE2, m_nEmployee.Phone2);
	DDX_Text(pDX, IDC_EDIT_EPY_EMAIL, m_nEmployee.Email);
	DDX_Text(pDX, IDC_EDIT_EPY_SALARY, m_nEmployee.Salary);
	DDX_Text(pDX, IDC_EDIT_EPY_ANNAUAL, m_nEmployee.Annaual);
	DDX_Text(pDX, IDC_EDIT_EPY_COMMISSION, m_nEmployee.Commission);
	DDX_Text(pDX, IDC_EDIT_EPY_REMARK, m_nEmployee.Remark);

	DDX_Control(pDX, IDC_CB_EPY_SEX, m_nCBSex);	
	DDX_Control(pDX, IDC_CB_EPY_TYPE, m_nCBType);	
	DDX_Control(pDX, IDC_CB_EPY_GRADE, m_nCBGrade);	
	DDX_Control(pDX, IDC_CB_EPY_CRADTYPE, m_nCBCardType);	
	DDX_Control(pDX, IDC_CB_EPY_EDUCATION, m_nCBEducation);

	DDX_Control(pDX, IDC_CB_EPY_GYEAR, m_nCBYear[GRADUATION_DAY]);	
	DDX_Control(pDX, IDC_CB_EPY_GMONTH, m_nCBMonth[GRADUATION_DAY]);	
	DDX_Control(pDX, IDC_CB_EPY_GDAY, m_nCBDay[GRADUATION_DAY]);	

	DDX_Control(pDX, IDC_CB_EPY_JYEAR, m_nCBYear[JOIN_DAY]);	
	DDX_Control(pDX, IDC_CB_EPY_JMONTH, m_nCBMonth[JOIN_DAY]);	
	DDX_Control(pDX, IDC_CB_EPY_JDAY, m_nCBDay[JOIN_DAY]);	

	DDX_Control(pDX, IDC_CB_EPY_BYEAR, m_nCBYear[BIRTH_DAY]);	
	DDX_Control(pDX, IDC_CB_EPY_BMONTH, m_nCBMonth[BIRTH_DAY]);	
	DDX_Control(pDX, IDC_CB_EPY_BDAY, m_nCBDay[BIRTH_DAY]);	

}


BEGIN_MESSAGE_MAP(CEmployeeTableDlg, CMaxDisplayDlg)
		ON_MESSAGE(RCV_MSG+EMPLOYEE_DLG_ID, &CEmployeeTableDlg::OnNetMessage)
		ON_BN_CLICKED(IDC_BTN_EMP_IMAGE, &CEmployeeTableDlg::OnBnClickedBtnEmpImage)
		ON_EN_CHANGE(IDC_EDIT_EPY_INDEX, &CEmployeeTableDlg::OnEnChangeEditEpyIndex)
		ON_EN_CHANGE(IDC_EDIT_EPY_ATTR, &CEmployeeTableDlg::OnEnChangeEditEpyAttr)
		ON_EN_CHANGE(IDC_EDIT_EPY_CARDNO, &CEmployeeTableDlg::OnEnChangeEditEpyCardNO)
		ON_EN_CHANGE(IDC_EDIT_EPY_PHONE1, &CEmployeeTableDlg::OnEnChangeEditEpyPhone1)
		ON_EN_CHANGE(IDC_EDIT_EPY_PHONE2, &CEmployeeTableDlg::OnEnChangeEditEpyPhone2)
		ON_EN_CHANGE(IDC_EDIT_EPY_EMAIL, &CEmployeeTableDlg::OnEnChangeEditEpyEmail)
		ON_EN_CHANGE(IDC_EDIT_EPY_SALARY, &CEmployeeTableDlg::OnEnChangeEditEpySalary)
		ON_EN_CHANGE(IDC_EDIT_EPY_ANNAUAL, &CEmployeeTableDlg::OnEnChangeEditEpyAnnaual)
		ON_EN_CHANGE(IDC_EDIT_EPY_COMMISSION, &CEmployeeTableDlg::OnEnChangeEditEpyCommission)
		ON_CBN_DROPDOWN(IDC_CB_EPY_GDAY, &CEmployeeTableDlg::OnCbnDropdownCbEpyBday)
		ON_CBN_DROPDOWN(IDC_CB_EPY_JDAY, &CEmployeeTableDlg::OnCbnDropdownCbEpyBday)
		ON_CBN_DROPDOWN(IDC_CB_EPY_BDAY, &CEmployeeTableDlg::OnCbnDropdownCbEpyBday)
		ON_BN_CLICKED(IDC_BTN_EMP_ADD, &CEmployeeTableDlg::OnBnClickedBtnEmpAdd)
		ON_BN_CLICKED(IDC_BTN_EMP_EDIT, &CEmployeeTableDlg::OnBnClickedBtnEmpEdit)
		ON_BN_CLICKED(IDC_BTN_EMP_DEL, &CEmployeeTableDlg::OnBnClickedBtnEmpDel)
		ON_BN_CLICKED(IDC_BTN_EMP_SRC, &CEmployeeTableDlg::OnBnClickedBtnEmpSrc)
		ON_BN_CLICKED(IDC_BTN_EMP_STATSTICS, &CEmployeeTableDlg::OnBnClickedBtnEmpStatstics)
END_MESSAGE_MAP()


// CEmployeeTableDlg message handlers

BOOL CEmployeeTableDlg::OnInitDialog()
{
	CMaxDisplayDlg::OnInitDialog();
	MSGWnd msgwnd;
	msgwnd.hwnd=GetSafeHwnd();
	msgwnd.index=0;
	RegisterNetMsg(&msgwnd, EMPLOYEE_DLG_ID);
	m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	RECT rc;
	::GetWindowRect(::GetDlgItem(m_hWnd, IDC_STATIC_EPY_IMAGE),&rc);
	ScreenToClient(&rc);
	m_wndImage.CreateImageWnd(&rc,this,IDC_STATIC_EPY_IMAGE);
	// Create the ToolTip control.
	m_tooltip.Create(this);
	m_tooltip.Activate(TRUE);
	m_tooltip.SetPopupSound(NULL);
	m_tooltip.AddTool(GetDlgItem(IDC_EDIT_EPY_NAME), IDS_MENU_EDIT_NAME);

	m_nCBSex.ResetContent();
	m_nCBSex.AddString(_T("Male"));
	m_nCBSex.AddString(_T("Female"));
	m_nCBSex.SetCurSel(0);

	m_nCBCardType.ResetContent();
	m_nCBCardType.AddString(_T("ID"));
	m_nCBCardType.AddString(_T("Parst Port"));
	m_nCBCardType.AddString(_T("Driver License"));
	m_nCBCardType.AddString(_T("Army License"));
	m_nCBCardType.AddString(_T("Other"));
	m_nCBCardType.SetCurSel(0);

	CString str;
	int i,j;
	CComboBox *p;

	for(i=0;i<MAX_DAY;i++)
	{
		p=&m_nCBYear[i];
		p->ResetContent();
		for(j=0;j<100;j++)
		{
			str.Format(_T("%d"), 1930+j);
			p->AddString(str);
		}
		p->SetCurSel(-1);

		p=&m_nCBMonth[i];
		p->ResetContent();
		for(j=1;j<=12;j++)
		{
			str.Format(_T("%d"), j);
			p->AddString(str);
		}
		p->SetCurSel(-1);
		
		p=&m_nCBDay[i];
		p->ResetContent();
		p->SetCurSel(-1);
	}
	
	UpdateData(false);
	return TRUE; 

}

LRESULT CEmployeeTableDlg::OnNetMessage(WPARAM wParam, LPARAM lParam)
{
	u8 *buf=(u8 *) wParam;
	u32 n=(u32) lParam;

	u32 cmd;
	memcpy(&cmd, buf, sizeof(cmd));

	switch(cmd)
	{
		case CMD_TADDEMP:
			//RxAddNewMenu((CMDRes *)(buf+sizeof(cmd)));
			break;
		case CMD_TEDITEMP:
			//RxAddNewMenu((CMDRes *)(buf+sizeof(cmd)));
			break;
		case CMD_TDELEMP:
			//RxAddNewMenu((CMDRes *)(buf+sizeof(cmd)));
			break;
		case CMD_TSRCEMP:
			//RxAddNewMenu((CMDRes *)(buf+sizeof(cmd)));
			break;
		default:
			break;
	}


	return 0;
}



void CEmployeeTableDlg::OnBnClickedBtnEmpImage()
{
	m_nImagePath = m_wndImage.SelImgFname();
}

void CEmployeeTableDlg::OnEnChangeEditEpyIndex()
{
	UpdateData(true);
	if(m_nEmployee.Index.GetLength()>5)
	{
		/*Notice error*/
		AfxMessageBox(_T("Index must be 5 digit."));
		GetDlgItem(IDC_EDIT_EPY_INDEX)->SetFocus();

	}
	else if(m_nEmployee.Index.GetLength()==0)
	{

	}
	else
	{
		/*add zero*/
		CString si=NULL;
		int n=5-m_nEmployee.Index.GetLength();
		for(int i=0;i<n;i++)
		{
			si+="0";
		}
		si+=m_nEmployee.Index;
		m_nEmployee.Index=si;
	}
	UpdateData(false);
}

void CEmployeeTableDlg::OnEnChangeEditEpyAttr()
{
	UpdateData(true);

	int   i; 
	int n;

	CString   sNewValue=m_nEmployee.Attr;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(!isalpha(sNewValue[i]))) 
		{
			break;   
		}
	}

	if(i<n)
	{
		m_nEmployee.Attr=m_nLastAbbr;
	}
	else
	{
		m_nLastAbbr=m_nEmployee.Attr;
	}
	UpdateData(false);
}

void CEmployeeTableDlg::OnEnChangeEditEpyCardNO()
{
	UpdateData(true);

	int   i; 
	int n;

	CString   sNewValue=m_nEmployee.CardNO;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(!isalpha(sNewValue[i]))) 
		{
			break;   
		}
	}

	if(i<n)
	{
		m_nEmployee.CardNO=m_nLastCardNO;
	}
	else
	{
		m_nLastCardNO=m_nEmployee.CardNO;
	}
	UpdateData(false);
}

void CEmployeeTableDlg::OnEnChangeEditEpyPhone1()
{
	UpdateData(true);

	int   i; 
	int n;

	CString   sNewValue=m_nEmployee.Phone1;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(!isalpha(sNewValue[i]))) 
		{
			break;   
		}
	}

	if(i<n)
	{
		m_nEmployee.Phone1=m_nLastPhone1;
	}
	else
	{
		m_nLastPhone1=m_nEmployee.Phone1;
	}
	UpdateData(false);
}
void CEmployeeTableDlg::OnEnChangeEditEpyPhone2()
{
	UpdateData(true);

	int   i; 
	int n;

	CString   sNewValue=m_nEmployee.Phone2;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(!isalpha(sNewValue[i]))) 
		{
			break;   
		}
	}

	if(i<n)
	{
		m_nEmployee.Phone2=m_nLastPhone2;
	}
	else
	{
		m_nLastPhone2=m_nEmployee.Phone2;
	}
	UpdateData(false);
}
void CEmployeeTableDlg::OnEnChangeEditEpyEmail()
{
}
void CEmployeeTableDlg::OnEnChangeEditEpySalary()
{
	UpdateData(true);

	int   i; 
	int n;
	int m=0;
	int a=0;

	CString   sNewValue=m_nEmployee.Salary;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(sNewValue[i]!='.')) 
		{
			break;   
		}
		if((sNewValue[i]=='.'))
		{
			if(m==0)
			{
				m++;
			}
			else
			{
				break;
			}
		}
		if((isdigit(sNewValue[i])&&(m==1)))
		{
			if(a<2)
			{
				a++;
			}
			else
			{
				break;
			}
		}

	}

	if(i<n)
	{
		m_nEmployee.Salary=m_nLastSalary;
	}
	else
	{
		m_nLastSalary=m_nEmployee.Salary;
	}
	UpdateData(false);

}
void CEmployeeTableDlg::OnEnChangeEditEpyAnnaual()
{
	UpdateData(true);

	int   i; 
	int n;
	int m=0;
	int a=0;

	CString   sNewValue=m_nEmployee.Annaual;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(sNewValue[i]!='.')) 
		{
			break;   
		}
		if((sNewValue[i]=='.'))
		{
			if(m==0)
			{
				m++;
			}
			else
			{
				break;
			}
		}
		if((isdigit(sNewValue[i])&&(m==1)))
		{
			if(a<1)
			{
				a++;
			}
			else
			{
				break;
			}
		}

	}

	if(i<n)
	{
		m_nEmployee.Annaual=m_nLastAnnaual;
	}
	else
	{
		m_nLastAnnaual=m_nEmployee.Annaual;
	}
	UpdateData(false);

}
void CEmployeeTableDlg::OnEnChangeEditEpyCommission()
{
	UpdateData(true);

	int   i; 
	int n;
	int m=0;
	int a=0;

	CString   sNewValue=m_nEmployee.Commission;   
	n=sNewValue.GetLength();
	for (i=0; i<n; i++)   
	{
		if((!isdigit(sNewValue[i]))&&(sNewValue[i]!='.')) 
		{
			break;   
		}
		if((sNewValue[i]=='.'))
		{
			if(m==0)
			{
				m++;
			}
			else
			{
				break;
			}
		}
		if((isdigit(sNewValue[i])&&(m==1)))
		{
			if(a<2)
			{
				a++;
			}
			else
			{
				break;
			}
		}

	}

	if(i<n)
	{
		m_nEmployee.Commission=m_nLastCommission;
	}
	else
	{
		m_nLastCommission=m_nEmployee.Commission;
	}
	UpdateData(false);

}

BOOL CEmployeeTableDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.
	{
		// Let the ToolTip process this message.
		m_tooltip.RelayEvent(pMsg);

		return CMaxDisplayDlg::PreTranslateMessage(pMsg);
	}
}

void CEmployeeTableDlg::GetControllerInfo(Employee *pEmployee)
{
	UpdateData(true);
	/*step1, check in the controllers*/	
	if(m_nEmployee.Index=="")
	{
		AfxMessageBox(_T("Name can not be null."));
		GetDlgItem(IDC_EDIT_EPY_NAME)->SetFocus();
		return;
	}
	if(m_nEmployee.Name=="")
	{
		AfxMessageBox(_T("Index must be 5 digit."));
		GetDlgItem(IDC_EDIT_EPY_NAME)->SetFocus();
		return;
	}
	m_nEmployee.Sex=(bool)m_nCBSex.GetCurSel();
	m_nEmployee.Type=m_nCBType.GetCurSel();	
	m_nEmployee.Grade=m_nCBGrade.GetCurSel();
	m_nEmployee.CardType=m_nCBCardType.GetCurSel();
	m_nEmployee.Education=m_nCBEducation.GetCurSel();

	for(u32 i=0;i<MAX_DAY;i++)
	{
		m_nEmployee.Year[i]=m_nCBYear[i].GetCurSel();
		m_nEmployee.Month[i]=m_nCBMonth[i].GetCurSel();
		m_nEmployee.Day[i]=m_nCBDay[i].GetCurSel();

		if(m_nEmployee.Year[i])
		{
			m_nEmployee.Year[i]+=1930;
		}
		if(m_nEmployee.Month[i])
		{
			m_nEmployee.Month[i]+=1;
		}
		if(m_nEmployee.Day[i])
		{
			m_nEmployee.Day[i]+=1;
		}
	}

	memcpy(pEmployee, &m_nEmployee, sizeof(m_nEmployee));

}


void CEmployeeTableDlg::SetControllerInfo(Employee *pEmployee)
{
	memcpy(&m_nEmployee, pEmployee, sizeof(m_nEmployee));

	m_nCBSex.SetCurSel((int)m_nEmployee.Sex);
	m_nCBType.SetCurSel(m_nEmployee.Type);	
	m_nCBGrade.SetCurSel(m_nEmployee.Grade);
	m_nCBCardType.SetCurSel(m_nEmployee.CardType);
	m_nCBEducation.SetCurSel(m_nEmployee.Education);

	for(u32 i=0;i<MAX_DAY;i++)
	{

		if(m_nEmployee.Year[i])
		{
			m_nEmployee.Year[i]-=1930;
		}
		if(m_nEmployee.Month[i])
		{
			m_nEmployee.Month[i]-=1;
		}
		if(m_nEmployee.Day[i])
		{
			m_nEmployee.Day[i]-=1;
		}

		m_nCBYear[i].SetCurSel(m_nEmployee.Year[i]);
		m_nCBMonth[i].SetCurSel(m_nEmployee.Month[i]);
		m_nCBDay[i].SetCurSel(m_nEmployee.Day[i]);

	}
	
	
	UpdateData(false);
}

void CEmployeeTableDlg::ClearControllerInfo(void)
{
	m_nEmployee.Index="";
	m_nEmployee.Name="";
	m_nEmployee.Attr="";
	m_nEmployee.Master="";
	m_nEmployee.School="";
	m_nEmployee.CardNO="";
	m_nEmployee.Phone1="";
	m_nEmployee.Phone2="";
	m_nEmployee.Email="";
	m_nEmployee.Salary="";
	m_nEmployee.Annaual="";
	m_nEmployee.Commission="";
	m_nEmployee.Remark="";

	for(u32 i=0;i<MAX_DAY;i++)
	{
		m_nEmployee.Year[i]=-1;
		m_nEmployee.Month[i]=-1;
		m_nEmployee.Day[i]=-1;

		m_nCBYear[i].SetCurSel(m_nEmployee.Year[i]);
		m_nCBMonth[i].SetCurSel(m_nEmployee.Month[i]);
		m_nCBDay[i].SetCurSel(m_nEmployee.Day[i]);

	}
	m_nEmployee.Sex=false;
	m_nEmployee.Type=-1;
	m_nEmployee.Grade=-1;
	m_nEmployee.CardType=-1;
	m_nEmployee.Education=-1;
	m_nCBSex.SetCurSel((int)m_nEmployee.Sex);
	m_nCBType.SetCurSel(m_nEmployee.Type);	
	m_nCBGrade.SetCurSel(m_nEmployee.Grade);
	m_nCBCardType.SetCurSel(m_nEmployee.CardType);
	m_nCBEducation.SetCurSel(m_nEmployee.Education);

	UpdateData(false);
}

bool  CEmployeeTableDlg::IsLeapYear(   int   Year   )   
  {   
          if(   Year   ==   0   )   
                  return   false;   
          else   
          {   
                  if(   Year%4   !=   0   )   
                          return   false;   
                  else   if(   Year%100   ==   0   )   
                          if(   Year%400   !=   0   )   
                                  return   false;   
          }   
    
          return   true;   
  }   


void CEmployeeTableDlg::OnCbnDropdownCbEpyBday()
{
	int year, month;
	CComboBox *p;
	CString str;
	int i,j, n;

	UpdateData(true);
	
	for(i=0;i<MAX_DAY;i++)
	{
		year=m_nCBYear[i].GetCurSel();
		month=m_nCBMonth[i].GetCurSel();

		if(year)
		{
			year+=1930;
		}
		if(month)
		{
			month+=1;
		}
		if(year&&month)
		{
			switch(month)
			{
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					n=31;
					break;
					
				case 4:
				case 6:
				case 9:
				case 11:
					n=30;
					break;
				case 2:
					if(IsLeapYear(year))
					{
						n=29;
					}
					else
					{
						n=28;
					}

			}

			p=&m_nCBDay[i];
			p->ResetContent();

			for(j=1;j<=n;j++)
			{
				str.Format(_T("%d"), j);
				p->AddString(str);
			}

		}
	}
	UpdateData(false);

}

void CEmployeeTableDlg::OnBnClickedBtnEmpAdd()
{
}
void CEmployeeTableDlg::OnBnClickedBtnEmpEdit()
{
}
void CEmployeeTableDlg::OnBnClickedBtnEmpDel()
{
}
void CEmployeeTableDlg::OnBnClickedBtnEmpSrc()
{
}
void CEmployeeTableDlg::OnBnClickedBtnEmpStatstics()
{
}

